package com.demo.web.test;

import java.lang.annotation.Annotation;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import com.apidoc.annotation.Api;
import com.apidoc.annotation.ApiAction;
import com.apidoc.enumeration.Method;

@RestController
@Api(name = "接口列表测试002" , order = 100)
public class UrlController {
	
	@Autowired
	private RequestMappingHandlerMapping requestMappingHandlerMapping;
	@Autowired
	private HttpServletRequest request;
 
	
	
	/**
	 * 获取当前项目中的全部接口
	 * */
	@ApiAction(name = "获取所有接口列表对象" , description = "获取所有接口列表对象获取所有接口列表对象获取所有接口列表对象获取所有接口列表对象")
	@RequestMapping(value = { "getAllUrl" , "allUrls" })
	@ResponseBody
	public Object getAllUrl() {
		StringBuilder sb = new StringBuilder();
		sb.append("URL").append("--").append("Class").append("--").append("Function").append('\n');
 
		Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
		int i = 1;
		for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
			RequestMappingInfo info = m.getKey();
			HandlerMethod method = m.getValue();
			sb.append(i+":").append(info.getClass().getName()).append("-")
			.append(info.getName()).
			append(info.getPatternsCondition()).append("--").append(info.getMethodsCondition()).append("--");
			sb.append(method.getMethod().getDeclaringClass()).append("--");
			sb.append(method.getMethod().getName()).append("--");
			
			sb.append( method.getMethod().getReturnType()).append("\n");
			ApiAction apiReqParams = method.getMethodAnnotation(ApiAction.class) ;
			
			
			if(apiReqParams != null) {
				sb.append( "apiReqParams:" + apiReqParams.name() ).append("\n");
			}
			
			for( Annotation ann : method.getMethod().getAnnotations() ) {
				sb.append( ann.annotationType().getSimpleName() ).append("--");
			}
			for( MethodParameter mp : method.getMethodParameters() ) {
				sb.append(mp.getParameterIndex() + "|" + mp.getParameterName() + "|" + mp.getParameterType()) ;
			}
			sb.append('\n');
			i++;
		}
 
		System.out.println(sb);
		
		return sb.toString() ;
 
	}
	/*
	 * 获取当前访问url
	 * */
	@ApiAction(name = "获取所有接口列表对象2" , description = "获取所有接口列表对象获取所有接口列表对象获取所有接口列表对象获取所有接口列表对象2")
	@RequestMapping(value = "getUrl")
	@ResponseBody
	public String getUrl() {
		String url = "";
		String scheme = request.getScheme();
		String serverName = request.getServerName();
		int serverPort = request.getServerPort();
		String servletPath = request.getServletPath();
		
		url = scheme +"://" + serverName +":" +serverPort+servletPath;
		if (request.getQueryString() != null){
			url += "?" + request.getQueryString();
		}
		
		System.out.println(url);
		
		return url;
	}


	
	@ApiAction(name = "获取所有接口列表对象4" , description = "获取所有接口列表对象获取所有接口列表对象获取所有接口列表对象获取所有接口列表对象2" , method = Method.GET)
	@PostMapping(value = "getUrlPost4")
	@ResponseBody
	public String getUrl4() {
		String url = "";
		String scheme = request.getScheme();
		String serverName = request.getServerName();
		int serverPort = request.getServerPort();
		String servletPath = request.getServletPath();
		
		url = scheme +"://" + serverName +":" +serverPort+servletPath;
		if (request.getQueryString() != null){
			url += "?" + request.getQueryString();
		}
		
		System.out.println(url);
		
		return url;
	}

	
	
	@ApiAction(name = "获取所有接口列表对象5" , description = "获取所有接口列表对象获取所有接口列表对象获取所有接口列表对象获取所有接口列表对象2")
	@RequestMapping(value = "getUrlPost5" , method = {RequestMethod.GET , RequestMethod.POST } )
	@ResponseBody
	public String getUrl3() {
		String url = "";
		String scheme = request.getScheme();
		String serverName = request.getServerName();
		int serverPort = request.getServerPort();
		String servletPath = request.getServletPath();
		
		url = scheme +"://" + serverName +":" +serverPort+servletPath;
		if (request.getQueryString() != null){
			url += "?" + request.getQueryString();
		}
		
		System.out.println(url);
		
		return url;
	}
}
